package com.tchintech.pay.view.activity;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.CodeDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.utils.CreateQRImageTest;
import com.tchintech.pay.utils.PathsUtil;
import com.tchintech.pay.utils.TimeUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2017/7/12 0012.
 *
 * 二维码展示页面
 *
 */

public class CodeActivity extends ActivityPresenter<CodeDelegate> implements View.OnClickListener {
    private CreateQRImageTest createQRImageTest;
    private ImageView codeImage;
    private String code = "";
    private String payType="";
    //二维码保存路径
    private  String codefile = PathsUtil.getRootPath(this) + PathsUtil.savecode("code");

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTitle("二维码收款");
        viewDelegate.setVisible();
        viewDelegate.setOnClickListener(this, R.id.bt_code, R.id.iv_back);
        createQRImageTest = new CreateQRImageTest();
        codeImage = viewDelegate.getCodeImage();
        //生成二维码
        System.out.println("开始生成二维码");
        createQRImageTest.createQRImage(code, codeImage);
    }

    @Override
    protected void initData() {
        super.initData();
        code = getIntent().getStringExtra("code");
        payType = getIntent().getStringExtra("payType");
        if(payType.equals("WX")){
            viewDelegate.setpaytype("请使用微信扫描");
        }else{
            viewDelegate.setpaytype("请使用支付宝扫描");
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_code:
                saveCodebitmap();
                break;
            case R.id.iv_back:
                finish();
                break;
        }

    }

    //保存图片到本地
    private void saveCodebitmap() {
        //二维码图片
        Bitmap bm = ((BitmapDrawable) ((ImageView) codeImage).getDrawable()).getBitmap();
        String name=TimeUtil.formatterToDates(TimeUtil.getNowMills()).replace("-","").replace(" ","").replace(":","")+".jpg";
            try {
                saveBitmap(bm,name);
            } catch (IOException e) {
                e.printStackTrace();
            }

    }
    public void saveBitmap(Bitmap bitmap,String name) throws IOException {
        //首先创建层级目录
        File file = new File(codefile);
        if(!file.exists()){
            file.mkdirs();
        }
        //根据层级目录来创建文件
        File files=new File(file,name);
        FileOutputStream out;
        System.out.println("得到的路径" + file);
        try {
            out = new FileOutputStream(files);
            System.out.println();
            if (bitmap.compress(Bitmap.CompressFormat.PNG, 70, out)) {
                out.flush();
                out.close();
                finishAllActivity();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭上个页面
     */
    private void finishAllActivity(){
        showMsg("保存成功");
        CodeReceiveActivity.activityList.get(0).finish();
        CodeReceiveActivity.activityList=null;
        finish();
    }

    @Override
    protected Class<CodeDelegate> getDelegateClass() {
        return CodeDelegate.class;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            CodeReceiveActivity.activityList=null;
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
